<html>
<head>
  <script src="../OLLoader.js"></script>
  <script type="text/javascript">

    function test_initialize(t) {
        t.plan(11);
        var control = new OpenLayers.Control.SLDSelect(OpenLayers.Handler.Click);
        t.eq(control.handler instanceof OpenLayers.Handler.Click, true, "Click handler created");
        t.ok(control.handler.callbacks["click"] === control.select, "Click callback correctly set");
        control.destroy();
        control = new OpenLayers.Control.SLDSelect(OpenLayers.Handler.RegularPolygon, {handlerOptions: {irregular: true}});
        t.eq(control.handler instanceof OpenLayers.Handler.RegularPolygon, true, "RegularPolygon handler created");
        t.eq(control.handler.irregular, true, "RegularPolygon handler is irregular");
        t.eq(control.handler.persist, false, "RegularPolygon handler is not persistant");
        t.ok(control.handler.callbacks["done"] === control.select, "Done callback correctly set");
        control.destroy();
        control = new OpenLayers.Control.SLDSelect(OpenLayers.Handler.Polygon);
        t.eq(control.handler instanceof OpenLayers.Handler.Polygon, true, "Polygon handler created");
        t.ok(control.handler.callbacks["done"] === control.select, "Done callback correctly set");
        control.destroy();
        control = new OpenLayers.Control.SLDSelect(OpenLayers.Handler.Path);
        t.eq(control.handler instanceof OpenLayers.Handler.Path, true, "Path handler created");
        t.ok(control.handler.callbacks["done"] === control.select, "Done callback correctly set");
        control.destroy();
        var layer = new OpenLayers.Layer.WMS('Foo', 'http://foo', {LAYERS: 'A'});
        control = new OpenLayers.Control.SLDSelect(OpenLayers.Handler.RegularPolygon, {layers: [layer]});
        t.eq(control.layers.length, 1, "Layers property correctly set");
        control.destroy();
        layer.destroy();
    }

    function test_select(t) {
        t.plan(9);
        var parser = new OpenLayers.Format.WFSDescribeFeatureType();
        var map = new OpenLayers.Map('map');
        var layer = new OpenLayers.Layer.WMS('Foo', 'http://foo', {LAYERS: 'AAA64'});
        map.addLayer(layer);

        var text =
            '<?xml version="1.0" encoding="ISO-8859-1" ?>' +
            '<schema' +
            '   targetNamespace="http://mapserver.gis.umn.edu/mapserver" ' +
            '   xmlns:rws="http://mapserver.gis.umn.edu/mapserver" ' +
            '   xmlns:ogc="http://www.opengis.net/ogc"' +
            '   xmlns:xsd="http://www.w3.org/2001/XMLSchema"' +
            '   xmlns="http://www.w3.org/2001/XMLSchema"' +
            '   xmlns:gml="http://www.opengis.net/gml"' +
            '   elementFormDefault="qualified" version="0.1" >' +
            '  <import namespace="http://www.opengis.net/gml"' +
            '          schemaLocation="http://schemas.opengis.net/gml/2.1.2/feature.xsd" />' +
            '  <element name="AAA64" ' +
            '           type="rws:AAA64Type" ' +
            '           substitutionGroup="gml:_Feature" />' +
            '  <complexType name="AAA64Type">' +
            '    <complexContent>' +
            '      <extension base="gml:AbstractFeatureType">' +
            '        <sequence>' +
            '          <element name="geometry" type="gml:MultiLineStringPropertyType" minOccurs="0" maxOccurs="1"/>' +
            '          <element name="OBJECTID" type="string"/>' +
            '        </sequence>' +
            '      </extension>' +
            '    </complexContent>' +
            '  </complexType>' +
            '</schema>';

        OpenLayers.Control.SLDSelect.prototype.wfsCache[layer.id] = parser.read(text);
        var control = new OpenLayers.Control.SLDSelect(OpenLayers.Handler.RegularPolygon,
            {layers: [layer], clearOnDeactivate: true, handlerOptions: {irregular: true} });

        var testEvent = function(evt) {
            t.eq(evt.filters.length, 1, "Event has a filters array set");
            t.eq(evt.filters[0] instanceof OpenLayers.Filter.Spatial, true, "Spatial filter has been created");
        };

        control.events.register("selected", this, testEvent);
        map.addControl(control);
        var geometry = OpenLayers.Geometry.Polygon.createRegularPolygon(
            new OpenLayers.Geometry.Point(0, 0), 5, 4);
        control.select(geometry);
        control.events.unregister("selected", this, testEvent);
        t.eq(map.layers.length, 2, "Selection layer has been created and added to the map");
        t.eq(map.layers[1] instanceof OpenLayers.Layer.WMS, true, "A WMS layer has been created as the selection layer");
        t.eq(map.layers[1].tileOptions.maxGetUrlLength, 2048, "Selection layer will automatically switch to HTTP Post if content gets longer than 2048");
        var expected_sld = '<sld:StyledLayerDescriptor xmlns:sld="http://www.opengis.net/sld" version="1.0.0" xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ogc="http://www.opengis.net/ogc" xmlns:gml="http://www.opengis.net/gml"><sld:NamedLayer><sld:Name>AAA64</sld:Name><sld:UserStyle><sld:Name>default</sld:Name><sld:FeatureTypeStyle><sld:Rule><ogc:Filter xmlns:ogc="http://www.opengis.net/ogc"><ogc:BBOX><ogc:PropertyName>geometry</ogc:PropertyName><gml:Box xmlns:gml="http://www.opengis.net/gml" srsName="EPSG:4326"><gml:coordinates decimal="." cs="," ts=" ">-3.5355339059327,-3.5355339059327 3.5355339059327,3.5355339059327</gml:coordinates></gml:Box></ogc:BBOX></ogc:Filter><sld:LineSymbolizer><sld:Stroke><sld:CssParameter name="stroke">#FF0000</sld:CssParameter><sld:CssParameter name="stroke-width">2</sld:CssParameter></sld:Stroke></sld:LineSymbolizer></sld:Rule></sld:FeatureTypeStyle></sld:UserStyle></sld:NamedLayer></sld:StyledLayerDescriptor>';

        t.xml_eq(map.layers[1].params.SLD_BODY, expected_sld, "SLD generated correctly");

        var geometry = OpenLayers.Geometry.Polygon.createRegularPolygon(
            new OpenLayers.Geometry.Point(0, 0), 7, 4);
        control.select(geometry);
        t.eq(map.layers.length, 2, "Selection layer is reused when new selection is performed");

        map.layers[0].setVisibility(false);
        t.eq(map.layers[1].getVisibility(), false, "Visibility of selection layer is synchronized with source layer");
        // activate would issue a SLD WMS DescribeLayer request and we are bypassing this here
        control.active = true;
        control.deactivate();
        t.eq(map.layers.length, 1, "Selection layer is removed on deactive if clearOnDeactivate is set to true");
        map.destroy();
    }

    function test_filterModificationOnSelected(t) {
        t.plan(1);
        var parser = new OpenLayers.Format.WFSDescribeFeatureType();
        var map = new OpenLayers.Map('map');
        var layer = new OpenLayers.Layer.WMS('Foo', 'http://foo', {LAYERS: 'AAA64'});
        map.addLayer(layer);

        var text =
            '<?xml version="1.0" encoding="ISO-8859-1" ?>' +
            '<schema' +
            '   targetNamespace="http://mapserver.gis.umn.edu/mapserver" ' +
            '   xmlns:rws="http://mapserver.gis.umn.edu/mapserver" ' +
            '   xmlns:ogc="http://www.opengis.net/ogc"' +
            '   xmlns:xsd="http://www.w3.org/2001/XMLSchema"' +
            '   xmlns="http://www.w3.org/2001/XMLSchema"' +
            '   xmlns:gml="http://www.opengis.net/gml"' +
            '   elementFormDefault="qualified" version="0.1" >' +
            '  <import namespace="http://www.opengis.net/gml"' +
            '          schemaLocation="http://schemas.opengis.net/gml/2.1.2/feature.xsd" />' +
            '  <element name="AAA64" ' +
            '           type="rws:AAA64Type" ' +
            '           substitutionGroup="gml:_Feature" />' +
            '  <complexType name="AAA64Type">' +
            '    <complexContent>' +
            '      <extension base="gml:AbstractFeatureType">' +
            '        <sequence>' +
            '          <element name="geometry" type="gml:MultiLineStringPropertyType" minOccurs="0" maxOccurs="1"/>' +
            '          <element name="OBJECTID" type="string"/>' +
            '        </sequence>' +
            '      </extension>' +
            '    </complexContent>' +
            '  </complexType>' +
            '</schema>';

        OpenLayers.Control.SLDSelect.prototype.wfsCache[layer.id] = parser.read(text);
        var control = new OpenLayers.Control.SLDSelect(OpenLayers.Handler.RegularPolygon,
            {layers: [layer], clearOnDeactivate: true, handlerOptions: {irregular: true} });

        var testEvent = function(evt) {
            // manipulate filter
            var bbox = OpenLayers.Bounds.fromString('1,2,3,4');
            evt.filters[0].value = bbox;
        };
        control.events.register("selected", this, testEvent);
        map.addControl(control);
        var geometry = OpenLayers.Geometry.Polygon.createRegularPolygon(
            new OpenLayers.Geometry.Point(0, 0), 5, 4);
        control.select(geometry);
        control.events.unregister("selected", this, testEvent);

        var expected_sld = '<sld:StyledLayerDescriptor xmlns:sld="http://www.opengis.net/sld" version="1.0.0" xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ogc="http://www.opengis.net/ogc" xmlns:gml="http://www.opengis.net/gml"><sld:NamedLayer><sld:Name>AAA64</sld:Name><sld:UserStyle><sld:Name>default</sld:Name><sld:FeatureTypeStyle><sld:Rule><ogc:Filter xmlns:ogc="http://www.opengis.net/ogc"><ogc:BBOX><ogc:PropertyName>geometry</ogc:PropertyName><gml:Box xmlns:gml="http://www.opengis.net/gml" srsName="EPSG:4326"><gml:coordinates decimal="." cs="," ts=" ">1,2 3,4</gml:coordinates></gml:Box></ogc:BBOX></ogc:Filter><sld:LineSymbolizer><sld:Stroke><sld:CssParameter name="stroke">#FF0000</sld:CssParameter><sld:CssParameter name="stroke-width">2</sld:CssParameter></sld:Stroke></sld:LineSymbolizer></sld:Rule></sld:FeatureTypeStyle></sld:UserStyle></sld:NamedLayer></sld:StyledLayerDescriptor>';

        t.xml_eq(map.layers[1].params.SLD_BODY, expected_sld, "Filter / SLD manipulated in select-callback correctly");

    }

    function test_multiselect(t) {
        t.plan(2);

        var parser = new OpenLayers.Format.WFSDescribeFeatureType();
        var map = new OpenLayers.Map('map');
        var layer = new OpenLayers.Layer.WMS('Multi', 'http://foo', {LAYERS: 'KGNAT.VKUNSTWERK,KGNAT.LKUNSTWERK,KGNAT.PKUNSTWERK'});
        map.addLayer(layer);

        var text =
            '<?xml version="1.0" encoding="ISO-8859-1" ?>' +
            '<schema' +
            '   targetNamespace="http://mapserver.gis.umn.edu/mapserver" ' +
            '   xmlns:rws="http://mapserver.gis.umn.edu/mapserver" ' +
            '   xmlns:ogc="http://www.opengis.net/ogc"' +
            '   xmlns:xsd="http://www.w3.org/2001/XMLSchema"' +
            '   xmlns="http://www.w3.org/2001/XMLSchema"' +
            '   xmlns:gml="http://www.opengis.net/gml"' +
            '   elementFormDefault="qualified" version="0.1" >' +
            '  <import namespace="http://www.opengis.net/gml"' +
            '          schemaLocation="http://schemas.opengis.net/gml/2.1.2/feature.xsd" />' +
            '  <element name="KGNAT.VKUNSTWERK" ' +
            '           type="rws:KGNAT.VKUNSTWERKType" ' +
            '           substitutionGroup="gml:_Feature" />' +
            '  <complexType name="KGNAT.VKUNSTWERKType">' +
            '    <complexContent>' +
            '      <extension base="gml:AbstractFeatureType">' +
            '        <sequence>' +
            '          <element name="geometry" type="gml:MultiPolygonPropertyType" minOccurs="0" maxOccurs="1"/>' +
            '        </sequence>' +
            '      </extension>' +
            '    </complexContent>' +
            '  </complexType>' +
            '  <element name="KGNAT.LKUNSTWERK" ' +
            '           type="rws:KGNAT.LKUNSTWERKType" ' +
            '           substitutionGroup="gml:_Feature" />' +
            '  <complexType name="KGNAT.LKUNSTWERKType">' +
            '    <complexContent>' +
            '      <extension base="gml:AbstractFeatureType">' +
            '        <sequence>' +
            '          <element name="geometry" type="gml:MultiLineStringPropertyType" minOccurs="0" maxOccurs="1"/>' +
            '        </sequence>' +
            '      </extension>' +
            '    </complexContent>' +
            '  </complexType>' +
            '  <element name="KGNAT.PKUNSTWERK" ' +
            '           type="rws:KGNAT.PKUNSTWERKType" ' +
            '           substitutionGroup="gml:_Feature" />' +
            '  <complexType name="KGNAT.PKUNSTWERKType">' +
            '    <complexContent>' +
            '      <extension base="gml:AbstractFeatureType">' +
            '        <sequence>' +
            '          <element name="geometry" type="gml:MultiPointPropertyType" minOccurs="0" maxOccurs="1"/>' +
            '        </sequence>' +
            '      </extension>' +
            '    </complexContent>' +
            '  </complexType>' +
            '</schema>';

        OpenLayers.Control.SLDSelect.prototype.wfsCache[layer.id] = parser.read(text);
        var control = new OpenLayers.Control.SLDSelect(OpenLayers.Handler.RegularPolygon, {handlerOptions: {irregular: true}, layers: [layer]});
        var testEvent = function(evt) {
            t.eq(evt.filters.length, 3, "Event has a filters array set with length tree");
        };
        control.events.register("selected", this, testEvent);

        map.addControl(control);
        var geometry = OpenLayers.Geometry.Polygon.createRegularPolygon(
            new OpenLayers.Geometry.Point(0, 0), 5, 4);
        control.select(geometry);
        var expected_sld = '<sld:StyledLayerDescriptor xmlns:sld="http://www.opengis.net/sld" version="1.0.0" xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ogc="http://www.opengis.net/ogc" xmlns:gml="http://www.opengis.net/gml"><sld:NamedLayer><sld:Name>KGNAT.VKUNSTWERK</sld:Name><sld:UserStyle><sld:Name>default</sld:Name><sld:FeatureTypeStyle><sld:Rule><ogc:Filter xmlns:ogc="http://www.opengis.net/ogc"><ogc:BBOX><ogc:PropertyName>geometry</ogc:PropertyName><gml:Box xmlns:gml="http://www.opengis.net/gml" srsName="EPSG:4326"><gml:coordinates decimal="." cs="," ts=" ">-3.5355339059327,-3.5355339059327 3.5355339059327,3.5355339059327</gml:coordinates></gml:Box></ogc:BBOX></ogc:Filter><sld:PolygonSymbolizer><sld:Fill><sld:CssParameter name="fill">#FF0000</sld:CssParameter></sld:Fill></sld:PolygonSymbolizer></sld:Rule></sld:FeatureTypeStyle></sld:UserStyle></sld:NamedLayer><sld:NamedLayer><sld:Name>KGNAT.LKUNSTWERK</sld:Name><sld:UserStyle><sld:Name>default</sld:Name><sld:FeatureTypeStyle><sld:Rule><ogc:Filter xmlns:ogc="http://www.opengis.net/ogc"><ogc:BBOX><ogc:PropertyName>geometry</ogc:PropertyName><gml:Box xmlns:gml="http://www.opengis.net/gml" srsName="EPSG:4326"><gml:coordinates decimal="." cs="," ts=" ">-3.5355339059327,-3.5355339059327 3.5355339059327,3.5355339059327</gml:coordinates></gml:Box></ogc:BBOX></ogc:Filter><sld:LineSymbolizer><sld:Stroke><sld:CssParameter name="stroke">#FF0000</sld:CssParameter><sld:CssParameter name="stroke-width">2</sld:CssParameter></sld:Stroke></sld:LineSymbolizer></sld:Rule></sld:FeatureTypeStyle></sld:UserStyle></sld:NamedLayer><sld:NamedLayer><sld:Name>KGNAT.PKUNSTWERK</sld:Name><sld:UserStyle><sld:Name>default</sld:Name><sld:FeatureTypeStyle><sld:Rule><ogc:Filter xmlns:ogc="http://www.opengis.net/ogc"><ogc:BBOX><ogc:PropertyName>geometry</ogc:PropertyName><gml:Box xmlns:gml="http://www.opengis.net/gml" srsName="EPSG:4326"><gml:coordinates decimal="." cs="," ts=" ">-3.5355339059327,-3.5355339059327 3.5355339059327,3.5355339059327</gml:coordinates></gml:Box></ogc:BBOX></ogc:Filter><sld:PointSymbolizer><sld:Graphic><sld:Mark><sld:WellKnownName>square</sld:WellKnownName><sld:Fill><sld:CssParameter name="fill">#FF0000</sld:CssParameter></sld:Fill><sld:Stroke/></sld:Mark><sld:Size>10</sld:Size></sld:Graphic></sld:PointSymbolizer></sld:Rule></sld:FeatureTypeStyle></sld:UserStyle></sld:NamedLayer></sld:StyledLayerDescriptor>';

        t.xml_eq(map.layers[1].params.SLD_BODY, expected_sld, "SLD generated correctly");
        map.destroy();
    }
    
  </script>
</head>
<body>
    <div id="map" style="width: 100px; height: 100px;"></div>
</body>
</html>
